<?php
//author: weibo.com/10coder 

require_once(dirname(__FILE__) . '/common/api3.class.php');
define('API3_BASE', dirname(__FILE__));
define('API3_BASE_MODELS', '/models/');
define('API3_CLASS_FILE', '.class.php');
define('API3_GLOBAL_NAME_BASE', '__api3__');
define('API3_GLOBAL_NAME_CLASS', API3_GLOBAL_NAME_BASE . 'classfile');

$GLOBALS[API3_GLOBAL_NAME_CLASS] = array();


//错误码定义
define('API3_ERR_NOFUN', 1001);
define('API3_ERR_HTTP', 1002);

define('API3_ERR_APNS_TOKEN', 2001);

//api3加载方法, model表示功能模块, driver表示不同的驱动器方法
function api3($model, $driver = 'base', $instance = true) {	
	$model_class = API3_BASE. API3_BASE_MODELS . $model . '/'. $model . '_' . $driver . API3_CLASS_FILE;
	if(!in_array($model_class, $GLOBALS[API3_GLOBAL_NAME_CLASS])) {
		if(file_exists($model_class)) {
			require_once($model_class);
			array_push($GLOBALS[API3_GLOBAL_NAME_CLASS], $model_class);	
		} else {
			return new api3_base();
		}
	}
	
	$class_name = 'api3_' . $model . '_' . $driver;
	$inst = null;
	
	if($instance) /*instance 单例*/ {
		$inst =  call_user_func_array(array($class_name, 'instance'), array());
	}	
	else /*factory 创建对象*/ {
		$inst =  new $class_name();
	}
	
	if(is_object($inst)) {
                return $inst;
        } else {
                return new api3_base();
        }

	return new api3_base();
}

